package com.cinyi.ormlite.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import com.cinyi.ormlite.ConfigurationException;
import com.cinyi.ormlite.IConfiguration;
import com.cinyi.ormlite.IContext;

/**
 * 构造web上下文对象
 * @author penghui
 *
 */
public class WebContext implements IContext{
	private ApplicationContext applicationContext;
	private WebConfiguration config = null ;
	
	public WebContext(ApplicationContext applicationContext){
		this.applicationContext = applicationContext;
		config = new WebConfiguration();
	}
	
	@Override
	public IConfiguration getConfiguration() {
		return config;
	}

	@SuppressWarnings("unchecked")
	public <T> T getBean(String name) throws BeansException {
		return (T) applicationContext.getBean(name);
	}

	@SuppressWarnings("unchecked")
    public <T> T getBean(Class<T> clazz) {
        return (T) applicationContext.getBeansOfType(clazz);
    }
	
	public ApplicationContext getApplicationContext() {
        return applicationContext ;
    }

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	public WebConfiguration getConfig() {
		return config;
	}

	public void setConfig(WebConfiguration config) {
		this.config = config;
	}

	@Override
	public void addBundle(String bundleName) throws ConfigurationException {
		config.addBundle(bundleName);
	}
	
}
